Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
How to Drag Form, like press Mouse on Form and Move it
Posted
Updated 1-Apr-11 1:35am
v2

What's going on here? Anyway, for the googlers I did also some copy paste for this trivial problem.

C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace MovableForm
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.Run(new MovableForm());
        }

        public partial class MovableForm : Form
        {
            int m_iWindowPosX;
            int m_iWindowPosY;

            public MovableForm()
            {
                FormBorderStyle = FormBorderStyle.None;
            }

            protected override void OnMouseDown(MouseEventArgs e)
            {
                m_iWindowPosX = e.X;
                m_iWindowPosY = e.Y;

                base.OnMouseDown(e);
            }

            protected override void OnMouseMove(MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                    Location = new Point(Location.X - m_iWindowPosX + e.X, Location.Y - m_iWindowPosY + e.Y);

                base.OnMouseMove(e);
            }
        }
    }
}
 
Share this answer
 
Try Follow:
C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool mousedown;
        int x, y;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = MousePosition.X - DesktopLocation.X;
                y = MousePosition.Y - DesktopLocation.Y;
                mousedown = true;
            }
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if(mousedown)
            {
                SetBounds(MousePosition.X - y, MousePosition.Y - y, Width, Height);
            }
        }
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            mousedown = false;
        }
    }
}
 
Share this answer
 
Comments
Tarun.K.S 1-Apr-11 7:41am    
What is SetBounds? And you answered the question in just 3 minutes after she posted the question (as if you know she will post it). Something's fishy.
Albin Abel 1-Apr-11 7:53am    
Ha ha, Copy pasting revealed. May be OP wants only coding than explanations.
Tarun.K.S 1-Apr-11 7:58am    
Exactly! I think both are related to each other. It was planned by both of them! Check bhavna's comments, messages and questions etc. Brijesh's answers were marked as solved. Check it out!
Albin Abel 1-Apr-11 8:02am    
Ha ha. Teacher student relationship revealed
Tarun.K.S 1-Apr-11 8:04am    
Hahahahaa! Wish I could upvote your comment! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900